<template>
  <el-dialog :destroy-on-close="true" :lock-scroll="false" title="申领" :visible.sync="visible" width="750px"
             :before-close="cancel">
    <el-form ref="form" :model="form" label-width="100px" :rules="rules">
      <el-row :gutter="15">
        <el-col :span="12">
          <el-form-item label="编码：">
            <el-input v-model="form.c_number" placeholder="自动带入" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="名称：">
            <el-input v-model="form.c_name" placeholder="自动带入" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类别：">
            <el-input v-model="form.type" placeholder="自动带入" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规格型号：">
            <el-input v-model="form.model" placeholder="自动带入" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计量单位：">
            <el-input v-model="form.uom" placeholder="自动带入" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="个人库存：">
            <el-input v-model="form.inventory" placeholder="自动带入" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仓库：">
            <el-input v-model="form.warehouse_name" placeholder="自动带入" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="库存数量：">
            <el-input v-model="form.inventory" placeholder="自动带入" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="分配类型：" prop="type">
            <el-input v-model="form.type" placeholder="自动带入" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="el-divider--horizontal" style="background: var(--border-color-lighter);margin: 20px 0"></div>
      <el-form-item label="申领数量：" prop="quantity">
        <el-input-number v-model="quantity" type="number" :min="0" :max="form.inventory<9999?form.inventory:9999"/>
      </el-form-item>
      <el-form-item label="说明：">
        <el-input v-model="form.description" placeholder="请输入备注" type="textarea" :rows="5"/>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认申领</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props: {
    // 修改回显的数据
    data: Object
  },
  data() {
    const validateQuantity = (rule, value, callback) => {
      if (this.quantity === 0) {
        callback(new Error('请输申领数量'));
      } else {
        callback();
      }
    };
    return {
      quantity: 0,
      editNum: null,
      // 弹窗是否打开
      visible: false,
      loading: false,
      // 表单数据
      form: {
        description: null,
      },
      rules: {
        quantity: [
          {required: true, validator: validateQuantity, trigger: 'change'},
        ],
      },
    }
  },
  methods: {
    active(info) {
      this.visible = true;
      this.form = {...info}
      this.form.description = ''
      this.form.type = '申领'

    },
    cancel() {
      this.visible = false;
      this.loading = false
      this.quantity= 0
      this.form = {
        quantity: 0
      }
    },
    /*保存*/
    save(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          this.form.items = [{id: this.form.id, quantity: this.quantity}]
          this.$http.post(`/inventories/apply`, this.form).then((res) => {
            if (res.data.code === 0) {
              this.$message.success(res.data.msg)
              this.$emit('reload')
            } else this.$message.error(res.data.msg)
            this.visible = false
            this.cancel()
          })
        }
      });
    },

  },
}
</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>
